package com.AUI
{
	import com.AUI.components.*;
	import com.AUI.events.*;
	import com.AUI.skins.blue.BlueStyle;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.utils.*;

	/**
	 * AUI Global Manager.
	 * @author HuangMinPing
	 * @site www.AminLab.com
	 */
	public class AUIGlobal
	{
		/**
		 * Current css.
		 */
		static private var _css:ACss = new BlueStyle;
		/**
		 * Get or set a current css for AUI component.
		 */
		static public function get currentCss():ACss
		{
			return _css;
		}
		static public function set currentCss(css:ACss):void
		{
			_css = css;
			/*
			// touch event depend on by order.
			var i:int;
			var len:int;
			var e:AGlobalEvent;
			var d:EventDispatcher = AUIPrivate.ED;
			e = new AGlobalEvent(AGlobalEvent.SKIN_ + 1);
			d.dispatchEvent(e);
			e = new AGlobalEvent(AGlobalEvent.SKIN_ + 2);
			d.dispatchEvent(e);
			e = new AGlobalEvent(AGlobalEvent.SKIN_ + 3);
			d.dispatchEvent(e);
			// Container class and its depth have collected complete,so touch SKIN_AFTER to container.
			e = new AGlobalEvent(AGlobalEvent.SKIN_AFTER);
			d.dispatchEvent(e);
			*/
		}
		/**
		 * Stage reference.
		 */
		static private var _stage:Stage;
		/**
		 * Get stage reference.
		 */
		static public function get stage():Stage
		{
			return _stage;
		}
		/**
		 * Set stage reference.
		 */
		static public function set stage(value:Stage):void
		{
			_stage = value;
		}
		/**
		 * Background mask layer.
		 */
		static private var _mask:Sprite = new Sprite;
		/**
		 * Singleton dialog instance.
		 */
		static private var _dialog:AComponent;
		/**
		 * Alert - show a singleton dialog of alert type.
		 * @param text The content of alert.
		 */
		static public function alert(message:String,callback_ok:Function = null):void
		{
			// close singleton dialog
			closeSingleDialog();
			// create mask layer
			_mask.graphics.clear();
			_mask.graphics.beginFill(0,0.5);
			_mask.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
			stage.addChild(_mask);
			// create singletom layer 
			_dialog = new Alert(message);
			_dialog.x = int((stage.stageWidth  - _dialog.width)  / 2);
			_dialog.y = int((stage.stageHeight - _dialog.height) / 2);
			stage.addChild(_dialog);
			// set callback handle.
			if(callback_ok == null)
			{
				Alert(_dialog).callback_ok = AUIGlobal.closeSingleDialog;
			}
		}
		/**
		 * Cloase current singleton dialog that is showed.
		 */
		static public function closeSingleDialog():void
		{
			if(_dialog != null)
			{
				stage.removeChild(_dialog);
				stage.removeChild(_mask);
				_dialog = null;
			}
		}
		/**
		 * Mapping.
		 */
		private static var _mapping:Dictionary;
		/**
		 * Singleton tip.
		 */
		private static var _tip:Label;
		/**
		 * Singleton tip holder.
		 */
		//private static var _stage:DisplayObjectContainer;
		
		/**
		 * Singleton Tip holder.
		 */
		//static public function set holder(value:DisplayObjectContainer):void
		//{
		//	_stage = value;
		//	_mapping = new Dictionary;
		//	AUIPrivate.addPreSelectorNames(".tip");
		//	_tip = new ALabel;
		//}
		/**
		 * Attach a tip to the target.
		 */
		public static function attach(target:DisplayObject,text:String):void
		{
			_mapping[target] = text;
			target.addEventListener(MouseEvent.ROLL_OVER,onRollOver,false,0,true);
			target.addEventListener(MouseEvent.ROLL_OUT,onRollOut,false,0,true);
			target.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove,false,0,true);
		}
		/**
		 * Detach a tip from the target.
		 */
		public static function detach(target:DisplayObject):void
		{
			target.removeEventListener(MouseEvent.ROLL_OVER,onRollOver);
			target.removeEventListener(MouseEvent.ROLL_OUT,onRollOut);
			target.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove,false,0,true);
			_mapping[target] = null;
		}
		/**
		 * @private
		 */
		private static function onRollOver(e:MouseEvent):void
		{
			_tip.value = _mapping[e.currentTarget];
			_stage.addChild(_tip);
		}
		/**
		 * @private
		 */
		private static function onRollOut(e:MouseEvent):void
		{
			_stage.removeChild(_tip);
		}
		/**
		 * @private
		 */
		private static function onMouseMove(e:MouseEvent):void
		{
			var x:int = e.stageX + 12;
			var y:int = e.stageY + 14;
			var w:int = _tip.width;
			var h:int = _tip.height;
			if(x + w  > _stage.stage.width)
			{
				_tip.x = x - w - 12;
			}
			else
			{
				_tip.x = x;
			}
			if(y + h > _stage.stage.height)
			{
				_tip.y = y - h - 14;
			}
			else
			{
				_tip.y = y;
			}
		}
	}
}